package com.alibaba.citrus.cr.order.open.api.facade.service.calculate;

import com.alibaba.citrus.cr.order.open.api.facade.common.model.WmsOutBoundMessage;
import com.alibaba.citrus.cr.order.open.api.facade.model.CalculateQuantityDTO;
import com.alibaba.citrus.cr.order.open.api.facade.model.TransferOrder;
import com.alibaba.citrus.cr.order.open.api.facade.transfer.TransferCenterCommonService;
import com.alibaba.cloud.retail.wms.api.model.orders.common.WarehousingOrderDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.allocateorderdomain.dto.UpdateStatusRequest;
import com.epoch.app.crunifiedinventory.allocateorderdomain.service.AllocateOrderDomainService;
import com.epoch.app.crunifiedinventory.domain.allocateapplyorderdetail.model.AllocateApplyOrderDetail;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.mninventorycenter.model.dto.SubFeaturesRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 货场提货单调拨数量处理器
 *
 * @author yifei
 * @since 2024/1/19
 */
@Service
public class TransferCalculateFreightYardTransferQuantityImpl extends TransferCalculateQuantityHandler{
    private static Log log = Log.getLogger(TransferCalculateFreightYardTransferQuantityImpl.class);
    @Resource
    private TransferCenterCommonService centerCommonService;
    @Resource
    private AllocateOrderDomainService allocateOrderDomainService;

    @Override
    public void handle(List<WarehousingOrderDTO> warehousingOrderList, WmsOutBoundMessage wmsOutBoundMessage, TransferOrder orderInfo) {
        // 获取当前调拨单计算数量合集
        CalculateQuantityDTO calculateQuantityDTO = CalculateCommonUtil.buildCalculateQuantityDTO(warehousingOrderList);
        // 获取当前出入库单的类型
        Integer mainOrderType = wmsOutBoundMessage.getWmsOrderDTO().getMainOrderType();
        // 获取当前单据要更新的数量
        Map<String, SubFeaturesRequest> currentUpdateMap = CalculateCommonUtil.buildCurrentUpdateMap(orderInfo, mainOrderType, calculateQuantityDTO);
        // 更新当前调拨单
        centerCommonService.updateTransferSubBizFeatures(wmsOutBoundMessage.getWmsOrderDTO().getBizOrderId(), currentUpdateMap);
        // 查询关联调拨单
        TransferOrder relatedOrderInfo = centerCommonService.getTransferOrderInfo(orderInfo.getRelatedAllocateOrderCode());
        if (relatedOrderInfo == null) {
            return;
        }
        // 根据关联调拨单号查询所有的货场提货单
        List<AllocateOrderDetail> allocateOrderDetailList = centerCommonService.getTransferOrderDetailListByRelatedCode(orderInfo.getRelatedAllocateOrderCode());
        if (CollectionUtils.isEmpty(allocateOrderDetailList)) {
            return;
        }
        // 构建更新关联调拨单的数量
        Map<String, SubFeaturesRequest> map = CalculateCommonUtil.buildRelatedTransferUpdateMap(allocateOrderDetailList);
        // 更新关联调拨单的数量
        centerCommonService.updateTransferSubBizFeatures(orderInfo.getRelatedAllocateOrderCode(), map);
        //更新调拨单本身主子单状态
        UpdateStatusRequest updateStatusRequest = UpdateStatusRequest.builder().code(orderInfo.getTransferOrderCode()).build();
        Result<Void> updateStatusResult = allocateOrderDomainService.updateStatus(updateStatusRequest);
        log.info("updateStatus_result:{}", JSON.toJSONString(updateStatusResult));
        // 获取计划调拨预报单，若计划调拨预报单不存在直接返回
        List<AllocateApplyOrderDetail> applyOrderDetailList = centerCommonService.getAllocateApplyOrderDetailList(relatedOrderInfo.getAllocateApplyOrderId());
        if (CollectionUtils.isEmpty(applyOrderDetailList)) {
            return;
        }
        // 通过计划调拨预报单号,查询所有的计划调拨单子单
        List<AllocateOrderDetail> transferOrderDetailList = centerCommonService.getTransferOrderDetailList(relatedOrderInfo.getAllocateApplyOrderId());
        if (CollectionUtils.isEmpty(transferOrderDetailList)) {
            return;
        }
        List<SubFeaturesRequest> transferUpdateList = CalculateCommonUtil.buildApplyTransferUpdateList(transferOrderDetailList, applyOrderDetailList,CalculateCommonUtil.FREIGHT_TRANSFER_TYPE);
        // 更新计划调拨预报单数量
        centerCommonService.updateApplyTransferSubBizFeatures(relatedOrderInfo.getAllocateApplyOrderId(), transferUpdateList);

    }

    @Override
    public String getTransferType(String transferType) {
        return CalculateCommonUtil.FREIGHT_TRANSFER_TYPE;
    }
}
